<?php
 
Loader::using("models::entities::CursoPk");
Loader::using("models::entities::Cursos");

/**
 * Description of ListaEstudiante
 *
 * @author ETorres
 * @export Modules
 * @export-metadata RequiredRoles:ROLE_PROFESSOR
 */
class EvaluarCurso extends Module {

    public function index() {
        
        $NIT = $this->currentUser->getName();
        
        $profesor = $this->model->teachers->GetById($NIT);
        
        if ($profesor != NULL){
            
            $Cursos = $this->model->courses->GetByDocente($NIT);
            
            $this->assign("Cursos", $Cursos);
            $this->assign("profesor", $profesor);
            $this->assign("action", $this->route->action("loadStudents"));
            
            $this->display("index");
            
        } else {
            
            $this->addMessage(new Message("Docente No Encontrado.", "", Message::ERROR));
            
            $this->display("noHayDocente");
        }
    }

    public function loadStudents() {
        Loader::using("models::entities::CursoPk");
        Loader::using("models::entities::PeriodoAcademico");

        $periodo = new PeriodoAcademico();
        $periodo->setAno($this->request->get("Ano"));
        $periodo->setPeriodo($this->request->get("Periodo"));

        $CursoPk = new CursoPk();

        $CursoPk->setCodigoMateria($this->request->get("CodigoMateria"));
        $CursoPk->setCodigoPlan($this->request->get("CodigoPlan"));
        $CursoPk->setGrupo($this->request->get("Grupo"));
        $CursoPk->setSemestre($this->request->get("Semestre"));
        $CursoPk->setPerdiodoAcademico($periodo);
        
        $CursosMatriculados = $this->model->registeredCourse->GetByCursoPk($CursoPk);
        
        $Estudiantes = $this->model->students->GetListaEstudianteByCurso($CursoPk);
        
        $cont = 0;        
        
        foreach ($CursosMatriculados as $CursoMatriculado) {
            
            $student = new Estudiantes();
            $student->setCodigoEstudiante($Estudiantes[$cont]->getCodigoEstudiante());
            $student->setApellidos($Estudiantes[$cont]->getApellidos());
            $student->setNombres($Estudiantes[$cont]->getNombres());

            $CursoMatriculado->setEstudiante($student);    
            
            $cont++;
            
        }
        
        $corteHabilitado = $this->model->calification->corteHabilitado($CursoPk);
                        
        $Curso = $this->model->courses->GetById($CursoPk);

        $this->assign("CursosMatriculados", $CursosMatriculados);
        $this->assign("corteHabilitado", $corteHabilitado);
        $this->assign("Curso", $Curso);
        $this->assign("action", $this->route->action("doEvaluarCurso"));
        $this->assign("cancel", $this->route->action("index"));

        $this->display();
    }

    public function doEvaluarCurso() { 
        if ($this->request->submitted()) {
            Loader::using("models::entities::CursosMatriculadosPk");

            $CursoPk = new CursoPk();

            $PeriodoAcademico = new PeriodoAcademico();

            $PeriodoAcademico->setAno($this->request->post("Ano"));
            $PeriodoAcademico->setPeriodo($this->request->post("Periodo"));

            $CursoPk->setPerdiodoAcademico($PeriodoAcademico);
            $CursoPk->setCodigoMateria($this->request->post("CodigoMateria"));
            $CursoPk->setCodigoPlan($this->request->post("CodigoPlan"));
            $CursoPk->setGrupo($this->request->post("Grupo"));
            $CursoPk->setSemestre($this->request->post("Semestre"));
            
            $corteHabilitado = $this->model->calification->corteHabilitado($CursoPk);
            
            if ($corteHabilitado != null){

                $Notas = $this->request->post("Notas");

                foreach ($Notas as $codigo => $Nota) {

                    $CursoMatriculadoPk = new CursosMatriculadosPk();

                    $CursoMatriculadoPk->setCursoPk($CursoPk);
                    $CursoMatriculadoPk->setCodigoEstudiante($codigo);

                    $this->model->calification->EvaluarCurso($CursoMatriculadoPk, $Nota, $corteHabilitado);
                }

                $this->addMessage(new Message("Los alumnos fueron calificados correctamente.", "", Message::SUCCESS));
                
            }else{
                
                $this->addMessage(new Message("No se ha calificado ning&uacute;n estudiante porque se cerr&oacute; el per&iacute;odo de calificaciones.", "", Message::INFO));
                
            }
        }

        $this->index();
    }

}

?>
